package com.lidx.back.statistics.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lidx.back.statistics.dao.IFilterRecordDao;
import com.lidx.back.statistics.entity.FilterRecord;
@Repository
public class FilterRecordDaoImpl implements IFilterRecordDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	public void insertRecord(FilterRecord record) {
		getSession().save(record);
	}

	public List<FilterRecord> getRecords() {
		return getSession().createQuery("from FilterRecord").list();
	}

	public List<Object[]> getRecordsWithParams(String action,
			String versionType, String from, String to) {
		StringBuffer sb = new StringBuffer();
		sb.append("select f.filterName,fr.versionType,count(1),f.id from FilterRecord fr,Filter f where fr.filterId = f.id");
		if(StringUtils.isNoneBlank(action)){
			sb.append(" and fr.action = '").append(action).append("'");
		}
		if(StringUtils.isNotBlank(versionType)){
			sb.append(" and fr.versionType = '").append(versionType).append("'");
		}
		if(StringUtils.isNotBlank(from) && StringUtils.isNotBlank(to)){
			sb.append(" and fr.recordTime between '").append(from).append("' and '").append(to).append("'");
		}
		sb.append("group by f.id");
		Query query = getSession().createQuery(sb.toString());
		return query.list();
	}

}
